package archer.application.elastic.mock.point.service;

import archer.application.elastic.mock.core.entity.user.Staff;
import archer.application.elastic.mock.core.entity.user.User;
import archer.application.elastic.mock.point.entity.PointDetail;
import archer.application.elastic.mock.point.entity.UserPoint;
import archer.framework.common.repository.condition.QueryItem;
import archer.framework.elastic.repository.condition.ElasticCondition;
import archer.framework.elastic.repository.condition.MultiMatchItem;
import archer.framework.elastic.service.BaseElasticService;
import archer.framework.protocol.result.ExecuteResult;
import com.avaje.ebean.Query;
import org.springframework.stereotype.Service;

/**
 * @author Dendi
 * @date 16/7/15
 */
@Service
public class UserPointElasticService extends BaseElasticService {

    // -----------------------------------------------------
    //                    crud
    // -----------------------------------------------------
    public ExecuteResult store() {
        return success();
    }

    public ExecuteResult deleteAll() {
        logicalDelete(User.find.all());
        return success();
    }

    public ExecuteResult delete(String id){
        logicalDelete(Staff.find.query().where().eq("id",id).findList());
        return success();
    }


    // -----------------------------------------------------
    //                    filter context
    // -----------------------------------------------------
    public ExecuteResult findUnique(String id) {
        return success(UserPoint.elastic.query().where().eq("id", id).findUnique());
    }

    public ExecuteResult findList() {
        return success(UserPoint.elastic.query().where().findList());
    }

    // -----------------------------------------------------
    //                    query context
    // -----------------------------------------------------

    public ExecuteResult findMust(QueryItem item) {
        return success(UserPoint.elastic.top(3, item).findList());
    }

    public ExecuteResult findMustNot(QueryItem item) {
        return success(UserPoint.elastic.text()
                .mustNot()
                .match(item.getField(), String.valueOf(item.getValue()))
                .findList());
    }

    public ExecuteResult findMultiMatch(MultiMatchItem multiMatchItem) {
        return success(UserPoint.elastic.text().multiMatch(multiMatchItem.getSearch(),
                multiMatchItem.asMultiMatch()).findList());
    }

    public ExecuteResult findTextList() {
        return success(UserPoint.elastic.query().text().findList());
    }

    // -----------------------------------------------------
    //                    UserPoint.elastic
    // -----------------------------------------------------

    public ExecuteResult all() {
        return success(UserPoint.elastic.all());
    }

    public ExecuteResult top(Integer rows, QueryItem item) {
        return success(UserPoint.elastic.top(rows, item).findList());
    }

    public ExecuteResult multiTop(Integer rows, MultiMatchItem item) {
        return success(UserPoint.elastic.multiTop(rows, item).findList());
    }

    // -----------------------------------------------------
    //                    docStore操作
    // -----------------------------------------------------
    public ExecuteResult indexByQuery(Query query) {
        UserPoint.elastic.docStore().indexByQuery(User.find.query());
        UserPoint.elastic.docStore().indexByQuery(UserPoint.find.query());
        UserPoint.elastic.docStore().indexByQuery(PointDetail.find.query());
        return success();
    }

    public ExecuteResult indexByQuery(Query query, int bulkSize) {
        UserPoint.elastic.docStore().indexByQuery(query, bulkSize);
        return success();
    }

    public ExecuteResult indexAll(Class clazz) {
        UserPoint.elastic.docStore().indexAll(clazz);
        return success();
    }

    public ExecuteResult dropIndex(String indexName) {
        UserPoint.elastic.docStore().dropIndex(indexName);
        return success();
    }


    // -----------------------------------------------------
    //                    page query
    // -----------------------------------------------------
    public ExecuteResult findElasticPagedList(ElasticCondition elasticCondition) {
        return success(findHitsPagedList(UserPoint.elastic.query().where().query(), elasticCondition));

    }

}
